package jzqb.service.impl;

import haha.life.common.BelongToSqlUtils;
import haha.life.common.CutPageBean;
import haha.life.common.SysTool;
import jzqb.bo.NoticeBo;
import jzqb.dao.AdminDepartmentDao;
import jzqb.dao.AdminUserInfoDao;
import jzqb.dao.INoticeDao;
import jzqb.po.AdminDepartmentPo;
import jzqb.po.AdminUserPo;
import jzqb.po.NoticePo;
import jzqb.service.INoticeService;
import jzqb.vo.NoticeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Description:
 *
 * @author BaiGe
 * @date: 2021/4/7 14:25
 * @Version 2.0
 */
@Service
public class NoticeServiceImpl implements INoticeService {

    @Autowired
    private INoticeDao noticeDao;
    @Autowired
    private AdminDepartmentDao departmentDao;
    @Autowired
    private AdminUserInfoDao userInfoDao;


    @Override
    public CutPageBean<NoticeVo> showNotice(NoticeBo noticeBo) {
        //设置时间格式
        String startTime = SysTool.dealTime(noticeBo.getStartTime(), SysTool.left);
        String endTime = SysTool.dealTime(noticeBo.getEndTime(), SysTool.right);
        noticeBo.setStartTime(startTime);
        noticeBo.setEndTime(endTime);

        CutPageBean<NoticeVo> cutPageBean = new CutPageBean<NoticeVo>();
        List<NoticeVo> noticeList = noticeDao.showNoticeList(noticeBo);
        //设置发布用户名
        if (noticeList != null && !noticeList.isEmpty()) {
            for (NoticeVo notice : noticeList) {
                String[] split = notice.getSenderId().split("_");
                if (split.length == 2) {
                    notice.setSenderName(split[1]);
                } else {
                    AdminUserPo user = userInfoDao.findUserById(split[0]);
                    notice.setSenderName(user.getName());
                }


            }
        }
        int count = (int) noticeDao.showNoticeCount(noticeBo);
        cutPageBean.initCutPage(count, noticeBo.getPageSize(), noticeList);
        return cutPageBean;
    }

    @Override
    public Boolean insertNotice(NoticePo noticePo) {
        if ("是".equals(noticePo.getIsTop())) {
            noticeDao.serAllIsTopToNo();
        }
        //发布用户存 id_name
        String senderId = noticePo.getSenderId();
        if (!"".equals(senderId) && senderId != null) {
            AdminUserPo user = userInfoDao.findUserById(senderId);
            String senderIdAndName = senderId + "_" + user.getName();
            noticePo.setSenderId(senderIdAndName);
        }
        return noticeDao.insertNotice(noticePo);
    }

    @Override
    public Boolean delNotice(Integer id) {
        return noticeDao.delNotice(id);
    }

    @Override
    public Boolean updateNotice(NoticePo noticePo) {
        if ("是".equals(noticePo.getIsTop())) {
          Boolean bl =  noticeDao.serAllIsTopToNo();
        }
        noticePo.setSendTime(new SimpleDateFormat(SysTool.dateFormat_milli).format(new Date()));
        return noticeDao.updateNotice(noticePo);
    }

    @Override
    public List<NoticeVo> getNoticeSort() {
        return noticeDao.getNoticeSort();
    }

    @Override
    public Boolean serAllIsTopToNo() {
        return null;
    }

    /*----------------------前台------------------------------*/
    @Override
    public CutPageBean<NoticeVo> showNoticeFront(NoticeBo noticeBo) {
        AdminUserPo user = userInfoDao.findUserById(noticeBo.getUserId());
        String deptStr = user.getDepartmentid();
        String belongToTargetDept = "";
        if (!"".equals(deptStr) && deptStr != null) {
            String[] deptArr = deptStr.split(";");
            belongToTargetDept = BelongToSqlUtils.getBelongToSql(deptArr, SysTool.YQTTZGG_TargetDept);
        }
        noticeBo.setBelongToTargetDept(belongToTargetDept);

        CutPageBean<NoticeVo> cutPageBean = new CutPageBean<>();
        List<NoticeVo> noticeList = noticeDao.findBelongAndNotAllNoticeList(noticeBo);
        if (noticeList != null && !noticeList.isEmpty()) {
            for (NoticeVo notice : noticeList) {
                String readUser = notice.getReadUser();
                if (!"".equals(readUser) && readUser != null) {
                    String[] readUserArr = readUser.split(";");
                    for (String str : readUserArr) {
                        if (str.equals(noticeBo.getUserId())) {
                            notice.setReadState(1);
                            break;
                        }
                    }
                } else {
                    notice.setReadState(0);
                }

            }
        }

        int count = (int) noticeDao.findBelongAndNotAllNoticeCount(noticeBo);
        cutPageBean.initCutPage(count, noticeBo.getPageSize(), noticeList);
        return cutPageBean;
    }

    @Override
    public NoticePo showSingleNotice(Integer noticeId) {
        NoticePo notice = noticeDao.findNoticeById(noticeId);
        return notice;
    }

    @Override
    public synchronized Boolean updateReadUser(Integer noticeId, String userId) {
        NoticePo notice = noticeDao.findNoticeById(noticeId);
        String newReadUser = "";
        String oleReadUser = notice.getReadUser();
        if ("".equals(oleReadUser) || oleReadUser == null) {
            newReadUser += userId;
        } else {
            newReadUser = oleReadUser + ";" + userId;
        }
        notice.setReadUser(newReadUser);
        Boolean bl = noticeDao.updateReadUser(notice);
        return bl;
    }

    @Override
    public long getUnReadNoticeNum(NoticeBo noticeBo) {
        AdminUserPo user = userInfoDao.findUserById(noticeBo.getUserId());
        String deptStr = user.getDepartmentid();
        String belongToTargetDept = "";
        if (!"".equals(deptStr) && deptStr != null) {
            String[] deptArr = deptStr.split(";");
            belongToTargetDept = BelongToSqlUtils.getBelongToSql(deptArr, SysTool.YQTTZGG_TargetDept);
        }
        noticeBo.setBelongToTargetDept(belongToTargetDept);
        return noticeDao.getUnReadNoticeNum(noticeBo);
    }


    @Override
    public CutPageBean<NoticeVo> showSystemNotice(NoticeBo noticeBo) {
        CutPageBean<NoticeVo> cutPage = new CutPageBean<>();
        List<NoticeVo> noticeList = noticeDao.findSystemNoticeList(noticeBo);
        if (noticeList != null && !noticeList.isEmpty()) {
            for (NoticeVo notice : noticeList) {
                //发送人
                String sender = notice.getSenderId();
                String[] split = sender.split("_");
                if (split.length == 2) {  //111_张三  格式存入 发布用户
                    notice.setSenderName(split[1]);
                }
            }
        }
        int count = (int) noticeDao.findSystemNoticeCount(noticeBo);
        cutPage.initCutPage(count, noticeBo.getPageSize(), noticeList);
        return cutPage;
    }

    @Override
    public List<NoticeVo> showSystemNoticeLimitFive() {
        List<NoticeVo> noticeList = noticeDao.showSystemNoticeLimitFive();
        SimpleDateFormat df = new SimpleDateFormat(SysTool.dateFormat_date);
        //将 2020-10-10 11:22:33  转换为 2020-10-10
        for (NoticeVo notice : noticeList) {
            if ("".equals(notice.getSendTime()) || notice.getSendTime() == null)
                continue;
            try {
                Date date = df.parse(notice.getSendTime());
                String SenderTime = df.format(date);
                notice.setSendTime(SenderTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return noticeList;
    }

    @Override
    public NoticeVo findSystemNoticeById(Integer noticeId) {
        NoticeVo notice = noticeDao.findSystemNoticeById(noticeId);
        if (notice != null) {
            String sender = notice.getSenderId();
            String[] spit = sender.split("_");
            if (spit.length == 2) {
                notice.setSenderName(spit[1]);
            }
        }
        return notice;
    }
}
